package com.example.springbootdemo.service;

import com.alibaba.fastjson.JSON;
import com.example.springbootdemo.bean.core.AnyConnectionManager;
import com.example.springbootdemo.bean.kernelApi.Result;
import com.example.springbootdemo.bean.kernelApi.ThingManySearchReq;
import com.example.springbootdemo.dto.project.ProjectResultObj;
import com.microsoft.signalr.HubConnection;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private AnyConnectionManager anyConnectionManager;

    @Override
    public ProjectResultObj getAJNProjectList(String projectId) {

        var type = new Result<ProjectResultObj>();
        var search = new ThingManySearchReq();
        search.setSkip((1 - 1) * 10);
        search.setTake(10);
//        search.getSort().add(new Sort("T470219805513879552", true));

        //通过属性筛选
/*        ArrayList<Object> groupData = new ArrayList<>();
        GroupObj groupObj = new GroupObj();
        groupObj.setSelector("T470219805513879552");
        groupObj.setIsExpanded(false);
        groupData.add(groupObj);
        search.setGroup(groupData);*/

     /*   LinkedList<String> strings = new LinkedList<>();
        strings.add("T470219805513879552");
        strings.add("=");
        strings.add("S470218641527410688");
*/
        LinkedList<List<String>> strings = new LinkedList<>();
        ArrayList<String> filterA = new ArrayList<>();
        ArrayList<String> filterB = new ArrayList<>();
        filterA.add("T470219805513879552");
        filterA.add("=");
        filterA.add("S470218641527410688");
        strings.add(filterA);
        filterB.add("T468899332339404800");
        filterB.add("=");
        filterB.add(projectId);
        strings.add(filterB);

        search.setFilter(strings);
        HubConnection anyHubConnection = anyConnectionManager.getHubConnection();
        anyConnectionManager.tokenAuthConnection();
//        hubConnection.invoke("TokenAuth", "1223244").blockingAwait();
        var  result = anyHubConnection.invoke(type.getClass(),"Load", "445723536485650432", search).blockingGet();
        var json = JSON.toJSONString(result);;
        ProjectResultObj result1 = JSON.parseObject(json, ProjectResultObj.class);
        return result1;
    }
}
